<?php
  class Array2Xml {

	//http://stackoverflow.com/questions/99350/passing-php-associative-arrays-to-and-from-xml
	public function buildXMLData($data, $startElement = 'fx_request', $xml_version = '1.0', $xml_encoding = 'UTF-8'){
		if(!is_array($data)){
		     $err = 'Invalid variable type supplied, expected array not found on line '.__LINE__." in Class: ".__CLASS__." Method: ".__METHOD__;
		trigger_error($err);
		if($this->_debug) echo $err;
			return false; //return false error occurred
		}
		$xml = new XmlWriter();
		$xml->openMemory();
		//$xml->startDocument($xml_version, $xml_encoding);
		$xml->startElement($startElement);
		/**
		 * Write XML as per Associative Array
		 * @param object $xml XMLWriter Object
		 * @param array $data Associative Data Array
		*/
		function write(XMLWriter $xml, $data){
			
			foreach($data as $position){
				$xml->startElement('position');				
				if(is_array($position)){
					$xml = ConvertToXml($position, $xml);
					$xml->endElement();
					continue;
				}
				$xml->endElement();
				$xml->writeElement($data, $position);
			}
		}
		
		function ConvertToXml ($data, $xml){
			
			foreach($data as $key => $value){			
				if(is_array($value)){

				$xml->startElement($key);				
				write($xml, $value);
				$xml->endElement();
				$xml->endElement();
				continue;
				}
			$xml->writeElement($key, $value);
			
			}
			return $xml;
		}
			
		
		write($xml, $data);
		
		$xml->endElement();//write end element
		//Return the XML results
		return $xml->outputMemory(true); 
	}
	
}
